import {saveGoodsToLive} from '../../../../utils/imapi'
var app = getApp();
Component({
  properties: {
      productList: {
          type:Array,
          value:[],
          observer:function(newVal, oldVal){
            if(!oldVal[0])return;
            for(var i = 0;i<oldVal.length;i++){

                if(newVal[i] && oldVal[i] && newVal[i].item_id==oldVal[i].item_id){
                    newVal[i] = oldVal[i]
                }
            }
            this.setData({
                productList:newVal
            })
          }
      },
      showTitle:Boolean,
      isLive:String
  },
  data: {
      productList: []
  },
  ready() {
      
  },
  methods: {
      //商品详情
      productDetail(e) {
        if(this.long_click){
            this.long_click = false
            return
        } 
          var id = e.currentTarget.dataset.id;
          var p = e.currentTarget.dataset.p;
          var index = e.currentTarget.dataset.index;
          app.globalData.cur_product = this.data.productList[index];
          wx.navigateTo({
              url: '/pages/module/productDetail/productDetail?id=' + id + '&p=' + p,
          })
      },
      imgLoad(e){
        var index = e.currentTarget.dataset.index;
        this.data.productList[index].imgShow=true;
        this.setData({
            productList:this.data.productList
        })
      },
      goHot(e){
          var index = e.currentTarget.dataset.index;
          if (this.data.productList[index].is_show_mask){
            var index = e.currentTarget.dataset.index;
            this.data.productList[index].is_show_mask=false;
            this.setData({
                productList:this.data.productList
            })
            wx.navigateTo({
              url: '/pages/module/hot/hot?q=' + this.data.productList[index].cid_name,
            })
          }
      
      },
        showMask(e){
            this.long_click = true
            var index = e.currentTarget.dataset.index;
            this.data.productList.forEach(item=>{
                item.is_show_mask=false;
            })
            this.data.productList[index].is_show_mask=true;
            this.setData({
                productList:this.data.productList
            })
        },
        hideMask(e){
            if(this.long_click){
                this.long_click = false
                return
            } 
            var index = e.currentTarget.dataset.index;
            this.data.productList[index].is_show_mask=false;
            this.setData({
                productList:this.data.productList
            })
        },
        add_live(e){
          console.log(1111111111111111111);
            // 添加到直播间
            let item= e.currentTarget.dataset.item;
            let p = item.p;
            let item_id=item.item_id;
            let title = item.title;
            
            let s="把"+p+" 上 "+title+"添加到直播间吗?"
            wx.showModal({
              title:'加入直播间?',
              content:s,
              success:async function(res){
                  if(res.confirm){


                    try {
                      wx.showLoading({
                        icon:'none',
                        title:'正在添加商品，请稍后'
                       })
                      // 调用添加到g_goods 并提审
                   const data =await saveGoodsToLive({
                     p:p,
                     item_id:item_id
                   })
                 
                   console.log(data);
                   //  确定加入
                   wx.showToast({
                     icon:'none',
                       title: '5分钟后 商品管理查看审核状态',
                   })
                   wx.hideLoading({})
                   return ;
                   }catch(err) {
                     wx.showToast({
                       icon:'none',
                       title:'商品未添加成功'
                     })
                   }
                  

                  }else {
                    wx.showToast({
                      title: '再看看其他宝贝',
                    })

                  }
              }
            })        


        }
  },
})